<template>
  <div class="background">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { nameRule, passRule } from "../utils/vaildate.js"; //通用校验 方便复用
import { setToken } from "../utils/setToken.js";
import { login } from "../api/api.js"; //接口封装地址
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: nameRule, trigger: "blur" }],
        password: [{ validator: passRule, trigger: "blur" }],
      },
    };
  },

  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
            // this.connector.post("/login", this.ruleForm).then((res) => {
            //   if (res.data.status === 200) {
            //     setToken("username", res.data.username);
            //     setToken("token", res.data.token);
            //     this.$message({ message: res.data.message, type: "success" });
            //     this.$router.push("/hello");
            //   }
            // });

            
        //   接口封装后调用
          login(this.ruleForm).then(res => {
            if (res.data.status === 200) {
              setToken("username", res.data.username);
              setToken("token", res.data.token);
              this.$message({ message: res.data.message, type: "success" });
              this.$router.push("/hello");
            }
          });
        } else {
          console.error(this.ruleForm);
        }
      });
    },
  },
};
</script>
<style lang="scss">
.background {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  width: 100%;
  height: 100%;
  background: pink;
  .el-form {
    width: 20%;
    height: 200px;
    position: absolute;
  }
}
</style>